package com.example.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.common.Result;
import com.example.dto.NoticeDTO;
import com.example.entity.Notices;
import com.example.service.NoticesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/notices")
public class NoticesController {

    @Autowired
    private NoticesService noticesService;

    @PostMapping("/list")
    public Result<IPage<Notices>> getAllNotices(@RequestBody NoticeDTO noticeDTO) {
        IPage<Notices> page = noticesService.getPage(noticeDTO);
        return Result.success(page);
    }

    @PostMapping("/create")
    public Result<Boolean> createNotice(@RequestBody Notices notice) {
        // 确保创建时间和更新时间设置正确
        notice.setCreatedAt(new Date());
        notice.setUpdatedAt(new Date());
        boolean success = noticesService.save(notice);
        return Result.success(success);
    }

    @PutMapping("/update/{id}")
    public Result<Boolean> updateNotice(@PathVariable Integer id, @RequestBody Notices notice) {
        notice.setId(id);
        // 仅更新更新时间
        notice.setUpdatedAt(new Date());
        boolean success = noticesService.updateById(notice);
        return Result.success(success);
    }

    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteNotice(@PathVariable Integer id) {
        boolean success = noticesService.removeById(id);
        return Result.success(success);
    }
}